//
//  LoginDetailView.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/10/16.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

#import "LoginDetailView.h"
#import "VerCodeViewController.h"
#import "RegFirstViewController.h"

@interface LoginDetailView() <TTTAttributedLabelDelegate, UIActionSheetDelegate>

@end

@implementation LoginDetailView

-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.backgroundColor = CSWhiteColor;
        
        /**
         登录Logo
         */
        UIImageView *loginImageView = [[UIImageView alloc] init];
        loginImageView.image = [UIImage imageNamed:@"regLogo"];
        [self addSubview:loginImageView];
        self.loginImageView = loginImageView;
        
        /**
         密码登录
         */
        UILabel *passWordLabel = [[UILabel alloc] init];
        passWordLabel.font = BoldBigFont;
        passWordLabel.text = @"账号登录";
        passWordLabel.textColor = CSColorFromRGB(0x0a0a0a);
//        [self addSubview:passWordLabel];
        self.passWordLabel = passWordLabel;
        
        NSMutableDictionary *attrs = [NSMutableDictionary dictionary];// 创建属性字典
        attrs[NSFontAttributeName] = MidBigFont; // 设置font
        attrs[NSForegroundColorAttributeName] = PlaceHoldColor; // 设置颜色
        
        /**
         手机TF
         */
        UITextField *phoneTF = [[UITextField alloc] init];
//        phoneTF.backgroundColor = CSRedColor;
        phoneTF.clearButtonMode=UITextFieldViewModeWhileEditing;
        //phoneTF.keyboardType = UIKeyboardTypeNumberPad;
        phoneTF.font = MidBigFont;
        phoneTF.textColor = CSColorFromRGB(0x212121);
        NSAttributedString *phoneAttStr = [[NSAttributedString alloc] initWithString:@"请输入您的账号" attributes:attrs]; // 初始化富文本占位字符串
        phoneTF.attributedPlaceholder = phoneAttStr;
        [self addSubview:phoneTF];
        self.phoneTF = phoneTF;
        phoneTF.leftViewMode = UITextFieldViewModeAlways;
        UIButton *userImage = [[UIButton alloc]initWithFrame:CGRectMake(10, 0, 40, 40)];
        
        userImage.userInteractionEnabled = NO;
        
//        userImage.contentEdgeInsets = UIEdgeInsetsMake(14, 14, 14, 14);
        
        [userImage setImage:[UIImage imageNamed:@"icon_mobilephone"] forState:UIControlStateNormal];
        
        phoneTF.leftView = userImage;
        
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = LineBackGroundColor;
        [self addSubview:lineView];
        self.lineView = lineView;
        
        /**
         密码TF
         */
        UITextField *passWordTF = [[UITextField alloc] init];
        passWordTF.secureTextEntry = YES;
        NSAttributedString *passAttStr = [[NSAttributedString alloc] initWithString:@"请输入密码,至少六位" attributes:attrs]; // 初始化富文本占位字符串
        passWordTF.attributedPlaceholder = passAttStr;
        passWordTF.font = MidBigFont;
        passWordTF.textColor = CSColorFromRGB(0x212121);
        [self addSubview:passWordTF];
        self.passWordTF = passWordTF;
        passWordTF.leftViewMode = UITextFieldViewModeAlways;
        
        UIButton *passImage = [[UIButton alloc]initWithFrame:CGRectMake(10, 0, 40, 40)];
        
        passImage.userInteractionEnabled = NO;
        
//        passImage.contentEdgeInsets = UIEdgeInsetsMake(14, 14, 14, 14);
        
        [passImage setImage:[UIImage imageNamed:@"icon_password"] forState:UIControlStateNormal];
        
        passWordTF.leftView = passImage;
        
        /**
         明文密文显示
         */
        UIButton *lookBtn = [[UIButton alloc] init];
        lookBtn.titleLabel.font = MidFont;
        [lookBtn setImage:[UIImage imageNamed:@"icon_eyes_gary"] forState:UIControlStateNormal];
        [lookBtn setImage:[UIImage imageNamed:@"icon_eyes"] forState:UIControlStateSelected];
        
        [self addSubview:lookBtn];
        self.lookBtn = lookBtn;
        
        UIView *lineView1 = [[UIView alloc] init];
        lineView1.backgroundColor = LineBackGroundColor;
        [self addSubview:lineView1];
        self.lineView1 = lineView1;
        
        /**
         忘记密码提示
         */
        TTTAttributedLabel *noticeLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
        noticeLabel.font = MidFont;
        noticeLabel.textColor = TextColor1;
        noticeLabel.delegate = self;
        //检测url
        noticeLabel.enabledTextCheckingTypes=NSTextCheckingTypeLink;
        noticeLabel.linkAttributes = @{(id)kCTForegroundColorAttributeName:TextColor1,NSUnderlineStyleAttributeName:@(1)}; //链接高亮状态文本属性
        noticeLabel.activeLinkAttributes = @{(id)kCTForegroundColorAttributeName:TextColor1,NSUnderlineStyleAttributeName:@(1)};
        [self addSubview:noticeLabel];
        self.noticeLabel = noticeLabel;

        NSString *text = @"忘记密码 >";

        [noticeLabel setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {

            NSRange boldRange = [[mutableAttributedString string] rangeOfString:text options:NSCaseInsensitiveSearch];

            //下划线
            [mutableAttributedString addAttribute:(NSString *)kCTUnderlineStyleAttributeName value:[NSNumber numberWithInt:kCTUnderlineStyleSingle]  range:boldRange];

            return mutableAttributedString;

        }];
        
//        添加url
        NSRange clickRange = [text rangeOfString:text options:NSCaseInsensitiveSearch];
        [noticeLabel addLinkToURL:[NSURL URLWithString:@"http://www.wangjimima.com"] withRange:clickRange];
        
        /**
         竖线
         */
        
        UIView *suLine = [UIView new];
        
        suLine.backgroundColor = TextColor1;
        
        [self addSubview:suLine];
        
        self.suLine = suLine;
        
        /**
         去注册新账号提示
         */
        TTTAttributedLabel *regLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
        regLabel.font = MidFont;
        regLabel.textColor = TextColor1;
        regLabel.delegate = self;
        //检测url
        regLabel.enabledTextCheckingTypes=NSTextCheckingTypeLink;
        regLabel.linkAttributes = @{(id)kCTForegroundColorAttributeName:TextColor1,NSUnderlineStyleAttributeName:@(1)}; //链接高亮状态文本属性
        regLabel.activeLinkAttributes = @{(id)kCTForegroundColorAttributeName:TextColor1,NSUnderlineStyleAttributeName:@(1)};
        [self addSubview:regLabel];
        self.regLabel = regLabel;
        
        NSString *regText = @"注册新账号 >";
        
        [regLabel setText:regText afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
            
            NSRange boldRange = [[mutableAttributedString string] rangeOfString:regText options:NSCaseInsensitiveSearch];
            
//            //下划线
//            [mutableAttributedString addAttribute:(NSString *)kCTUnderlineStyleAttributeName value:[NSNumber numberWithInt:kCTUnderlineStyleSingle]  range:boldRange];
            
            return mutableAttributedString;
            
        }];
        
        //        添加url
        NSRange regClickRange = [regText rangeOfString:regText options:NSCaseInsensitiveSearch];
        [regLabel addLinkToURL:[NSURL URLWithString:@"http://www.reg.com"] withRange:clickRange];
        
        
        /**
         登录按钮
         */
        UIButton *loginBtn = [[UIButton alloc] init];
        loginBtn.backgroundColor = ButtonBackGroundColor;
        loginBtn.layer.cornerRadius = 4;
        loginBtn.layer.masksToBounds = YES;
        [loginBtn setTitleColor:CSBlackColor forState:UIControlStateNormal];
        [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        loginBtn.titleLabel.font = BigFont;
        [self addSubview:loginBtn];
        self.loginBtn = loginBtn;
        
        [self setConstraints];
    }
    
    return self;
}

-(void)setConstraints{
    
    /**
     登录Logo
     */
    [self.loginImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
//        make.bottom.mas_equalTo(self.mas_centerY).offset(-80);
        
        make.top.mas_equalTo(self).offset(30);
        
        make.centerX.mas_equalTo(self);
    }];
    
    
    /**
     密码登录
     */
//    [self.passWordLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
//       
//        make.top.mas_equalTo(self).offset(15);
//        
//        make.left.mas_equalTo(self).offset(25);
//        
//        make.height.mas_equalTo(60);
//    }];
    
    
    /**
     手机TF
     */
    [self.phoneTF mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.loginImageView.mas_bottom).offset(30);
        
        make.left.mas_equalTo(self).offset(25);
        
        make.right.mas_equalTo(self).offset(-25);
        
        make.height.mas_equalTo(40);
    }];
    
    [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.width.mas_equalTo(self.phoneTF);
        
        make.height.mas_equalTo(1);
        
        make.top.mas_equalTo(self.phoneTF.mas_bottom).offset(10);
    }];
    
    /**
     密码TF
     */
    [self.passWordTF mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(15);
        
        make.left.width.height.mas_equalTo(self.phoneTF);
        
    }];

    /**
     明文密文显示
     */
    [self.lookBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self.passWordTF).offset(-5);
        
        make.centerY.mas_equalTo(self.passWordTF);
    }];
    
    [self.lineView1 mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.width.mas_equalTo(self.passWordTF);
        
        make.height.mas_equalTo(1);
        
        make.top.mas_equalTo(self.passWordTF.mas_bottom).offset(10);
    }];
    
    /**
     登录按钮
     */
    [self.loginBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.lineView1.mas_bottom).offset(35);
        
        make.left.mas_equalTo(self).offset(30);
        
        make.right.mas_equalTo(self).offset(-30);
        
        make.height.mas_equalTo(50);
        
    }];
    
    //去注册
    [self.regLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self.mas_centerX).offset(-10);
        
        make.top.mas_equalTo(self.loginBtn.mas_bottom).offset(15);
        
    }];
    
    /**
     竖线
     */
    [self.suLine  mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(self);
        
        make.top.mas_equalTo(self.loginBtn.mas_bottom).offset(15);
        
        make.width.mas_equalTo(1);
        
        make.height.mas_equalTo(self.regLabel);
    }];
    
    /**
     忘记密码提示
     */
    [self.noticeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(self.mas_centerX).offset(10);

        make.top.mas_equalTo(self.loginBtn.mas_bottom).offset(15);
    }];
    
}

- (void)attributedLabel:(TTTAttributedLabel*)label didSelectLinkWithURL:(NSURL*)url{
    
    NSLog(@"打印设置的URL%@进行跳转处理",url);
    
    NSString *strUrl = [NSString stringWithFormat:@"%@",url];
    
    if([strUrl isEqualToString:@"http://www.wangjimima.com"]){
        
//        ForgetPassViewController *forgetVC = [[ForgetPassViewController alloc] init];
//
//        [self.viewController.navigationController pushViewController:forgetVC animated:YES];
        
        VerCodeViewController *verCodeVC = [[VerCodeViewController alloc] init];

        verCodeVC.from = @"忘记密码";
        
        [self.viewController.navigationController pushViewController:verCodeVC animated:YES];
    }else
    {
        
        //注册新账号
        RegFirstViewController *regVC = [RegFirstViewController new];
        
        [self.viewController.navigationController pushViewController:regVC animated:YES];
        
    }
}

@end
